@extends('admin.layouts.layout')

@section('title', '首页')

@section('css')
    <link href="{{loadEdition('/admin/css/pxgridsicons.min.css')}}" rel="stylesheet"/>
@endsection
@section('content')

    <?php
    use App\Handlers\GameChannelHelper;
    use App\Models\Enum\Judge;
    ?>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox-title">
                <h5>
                    <a href="javascript:;" data-toggle="tooltip" data-placement="bottom"
                       title="">核算</a></h5>
            </div>
            <div class="ibox-content">

                <form class="form-horizontal" action="{{route('finance.check.index')}}" method="get"
                      enctype="multipart/form-data">
                    <table class="table-striped table-bordered table-hover table-condensed">
                        <tr class="b-group">
                            <td class="b-child" colspan="2">
                                <table class="table-striped table-bordered table-hover table-condensed">
                                    <tr>
                                        <td>
                                            @include('widget.between_datetime',['showEndTime'=>true])
                                        </td>
                                        <td>
                                            @include('widget.query_and_refresh')
                                        </td>
                                        <td>
                                            <button class="btn-sm btn-default" type="button"
                                                    onclick="javascript:clearRadio();"><i
                                                        class="fa fa-repeat"></i> 清 理
                                            </button>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th style="min-width: 108px;">
                                游戏
                            </th>
                            <td class="b-child">
                                <div style="max-height:6.5rem;overflow-y:scroll;">
                                    @include('widget.game_radio')
                                </div>
                            </td>
                        </tr>
                        <tr class="b-group">
                            <th style="min-width: 108px;">
                                渠道
                            </th>
                            <td class="b-child">
                                <div style="max-height:6.5rem;overflow-y:scroll;">
                                    @include('widget.channel_radio_all')
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>

                <table class="table table-bordered table-striped tblData" data-toggle="table"
                       data-pagination="true">
                    <thead>
                    <tr>
                        <th>
                            游戏名称
                        </th>
                        <th>
                            渠道
                        </th>
                        <th>
                            总收入(巨枫)
                        </th>
                        <th>
                            总收入(渠道)
                        </th>
                        <th>
                            <a href="javascript:;" data-toggle="tooltip" data-placement="top" title="总收入(巨枫) 减 总收入(渠道)">差异(百分比)</a>
                        </th>
                        <th>
                            合同约定的结算时间
                        </th>
                        <th>分成类型</th>
                        <th>分成比例</th>
                        <th>公司实收金额</th>
                        <th>应收金额(渠道)</th>
                        <th>商务确认</th>
                        <th>财务开票确认</th>
                        <th>财务到账确认</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $html = '';
                    foreach ($incomes as $value) {
                        $key = $value['game_id'] . '-' . $value['channel_id'];

                        $href = route('check.create', [
                            'start_time' => $reqStartTime,
                            'end_time' => $reqEndTime,
                            'game_id' => $value['game_id'],
                            'channel_id' => $value['channel_id'],
                        ]);

                        $diff = 0;
                        $diffPercent = 0;
                        $income = 0;
                        $contractTime = '';
                        $shareRatio = '';
                        $AmountReceivable = 0;
                        $realIncome = 0;

                        $noHtml = '<span class=\'text-danger\'>' . Judge::get(Judge::NO) . '</span>';
                        $yesHtml = '<span class=\'text-navy\'>' . Judge::get(Judge::YES) . '</span>';
                        $businessConfirm = $noHtml;
                        $receiptConfirm = $noHtml;
                        $arriveConfirm = $noHtml;
                        $shareRatioType = '';


                        if (isset($checkInfo[$key])) {
                            $income = $checkInfo[$key]['income'];
                            $contractTime = $checkInfo[$key]['contract_time'];

                            //分成比例
                            $shareRatioTd = '';
                            foreach ($checkInfo[$key]['ratios'] as $ratio) {
                                $self_share_ratio = ($ratio['self_share_ratio'] * 100) . '%';
                                $channel_share_ratio = ($ratio['channel_share_ratio'] * 100) . '%';
                                $shareRatioTd .= "<tr><td>{$ratio['min_value']}</td><td>{$ratio['max_value']}</td><td>{$self_share_ratio}</td><td>{$channel_share_ratio}</td></td>";
                            }
                            $shareRatio = '<table class="table-striped table-bordered table-hover table-condensed"><thead><th>最小值</th><th>最大值</th><th>巨枫</th><th>渠道</th></thead>' . $shareRatioTd . '</table>';

                            //应收金额(渠道)
                            $AmountReceivable = 0;
                            if ($checkInfo[$key]['share_ratio_type'] == \App\Models\Enum\ShareRatioType::SEPARATE) {
                                foreach ($checkInfo[$key]['ratios'] as $ratio) {
                                    if ($income >= $ratio['min_value'] &&
                                        (($ratio['max_value'] !== 0 && $income < $ratio['max_value']) || ($ratio['max_value'] == 0))) {
                                        $AmountReceivable = $ratio['channel_share_ratio'] * $income;
                                        break;
                                    }
                                }
                            } else if ($checkInfo[$key]['share_ratio_type'] == \App\Models\Enum\ShareRatioType::LADDER) {
                                foreach ($checkInfo[$key]['ratios'] as $ratio) {
                                    if ($income > $ratio['min_value']) {
                                        $min = ($ratio['max_value'] > 0) ? min($income, $ratio['max_value']) - $ratio['min_value'] : $income - $ratio['min_value'];
                                        $AmountReceivable = $AmountReceivable + ($ratio['channel_share_ratio'] * $min);
                                    }
                                }
                            } else {
                                $AmountReceivable = '分成比例类型:未定义';
                            }

                            $businessConfirm = $checkInfo[$key]['business_confirm'] == Judge::YES ? $yesHtml : $noHtml;
                            $receiptConfirm = $checkInfo[$key]['receipt_confirm'] == Judge::YES ? $yesHtml : $noHtml;
                            $arriveConfirm = $checkInfo[$key]['arrive_confirm'] == Judge::YES ? $yesHtml : $noHtml;
                            $diff = $value['total_amounts'] - $income;
                            $diffPercent = ($income > 0) ? round($diff / $income * 100, 2) . '%' : 0;
                            $href = route('check.edit', $checkInfo[$key]['id']);
                            $realIncome = $checkInfo[$key]['real_income'];
                            $shareRatioType = \App\Models\Enum\ShareRatioType::get($checkInfo[$key]['share_ratio_type']);
                        }

                        $updateHtml = '<a href="' . $href . '"><button class="btn btn-primary btn-xs" type="button"><i class="fa fa-paste"></i> 修改</button></a>';
                        $gameName = GameChannelHelper::getGameName($value['game_id']);
                        $channelName = GameChannelHelper::getChannelName($value['game_id'], $value['channel_id']);
                        $html .= "<tr>
                        <td>{$gameName}</td>
                        <td>{$channelName}</td>
                        <td>{$value['total_amounts']}</td>
                        <td>{$income}</td>
                        <td>{$diff}({$diffPercent})</td>
                        <td>{$contractTime}</td>
                        <td>{$shareRatioType}</td>
                        <td>{$shareRatio}</td>
                        <td>{$realIncome}</td>
                        <td>{$AmountReceivable}</td>
                        <td>{$businessConfirm}</td>
                        <td>{$receiptConfirm}</td>
                        <td>{$arriveConfirm}</td>
                        <td>{$updateHtml}</td>
                        </tr>";
                    }
                    echo $html;
                    ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    </div>
    <link href="{{loadEdition('/admin/css/plugins/datapicker/bootstrap-datepicker.css')}}" rel="stylesheet">
@stop
@section('js')
    <script src="{{loadEdition('/admin/js/plugins/datapicker/bootstrap-datepicker.js')}}"></script>
    <script src="{{loadEdition('/admin/js/plugins/datapicker/bootstrap-datepicker.zh-CN.js')}}"></script>
    <script src="{{loadEdition('/admin/js/plugins/datapicker/bootstrap-datepicker.zh-CN.js')}}"></script>
    <!--bootstrap-table start-->
    <script src="{{loadEdition('/admin/js/plugins/bootstrap-table/bootstrap-table.min.js')}}"></script>
    <script src="{{loadEdition('/admin/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.js')}}"></script>
    <!--bootstrap-table end-->
    <script src="{{loadEdition('/js/global.js')}}"></script>
    <script>

        //游戏checkbox
        $('#tdGames').find(':checkbox').change(function () {
            var game_id = $(this).val();
            if (this.checked) {
                $('#tdChannels div[data-game-id="' + game_id + '"]').show().find("input:checkbox").prop('checked', true);
            } else {
                $('#tdChannels div[data-game-id="' + game_id + '"]').hide().find("input:checkbox").prop('checked', false);
            }
        });

        function clearRadio() {
            $('input:radio').prop('checked', false);
        }

    </script>
@stop
